import React from 'react';
import { Form, Row, Col, Input, Radio } from 'antd';
import {
    COMMUNICATE_RESULT,
    COMMUNICATE_RESULT_ENUM,
    DEPARTMENT_SUGGEST_TYPE_ENUM
} from '@/pages/Recruitment/dictionary';
import cn from 'classnames';
import { formatToThreePart } from '@/utils/utils';

import styles from '../index.less';

const { TextArea } = Input;

const newOneLineLayout = {
    labelCol: { span: 9 },
    wrapperCol: { span: 15 }
};

const getInintRemark = (commInfo, isSecond) => {
    if (isSecond && commInfo.isSave === 0) {
        // 二次谈薪且非暂存数据，则不回显
        return '';
    }
    return commInfo?.remark;
};

const labelStyle = {
    fontSize: '16px',
    fontWeight: 600,
    marginRight: '-3px'
};

function Result({ form, commInfo, isEdit, isSecond, oneLineLayout, talentInfo, isSecondRecord }) {
    /* isDepart 由请求参数中的type=dept 控制 */
    const { getFieldDecorator } = form;
    const showStatus = !!commInfo?.recruitDeptRecordVO?.suggestedStatus;
    const showSuggestSalary = commInfo?.recruitDeptRecordVO?.suggestedStatus === DEPARTMENT_SUGGEST_TYPE_ENUM.CHANGE;
    /**
     * 用人部门的建议
     */
    const recruitDeptContent = () => {
        if (showStatus) {
            return (
                <>
                    <Col span={24}>
                        <Form.Item
                            colon={false}
                            label={<span style={labelStyle}>用人部门建议:</span>}
                            {...oneLineLayout}
                        >
                            <span
                                className={cn(styles.hireStatus, {
                                    [styles.hireStatusOut]:
                                        commInfo?.recruitDeptRecordVO?.suggestedStatus ===
                                        DEPARTMENT_SUGGEST_TYPE_ENUM.OUT
                                })}
                            >
                                {commInfo?.recruitDeptRecordVO?.resultName}
                            </span>
                        </Form.Item>
                    </Col>
                    {showSuggestSalary && (
                        <>
                            <Col span={8}>
                                <Form.Item label="新建议年薪" {...newOneLineLayout}>
                                    {formatToThreePart(commInfo?.recruitDeptRecordVO?.suggestedYearSalary)}{' '}
                                    {talentInfo.employSalaryCurrencyCode || talentInfo.companyCurrencyCode}
                                </Form.Item>
                            </Col>
                            <Col span={8}>
                                <Form.Item label="新建议月薪" {...newOneLineLayout}>
                                    {formatToThreePart(commInfo?.recruitDeptRecordVO?.suggestedMonthSalary)}
                                    <span style={{ padding: '0 5px' }}>X</span>
                                    {commInfo?.recruitDeptRecordVO?.suggestedMonthNumber}{' '}
                                    {talentInfo.employSalaryCurrencyCode || talentInfo.companyCurrencyCode || '月'}
                                </Form.Item>
                            </Col>
                            <Col span={8}>
                                <Form.Item label="分数位" {...newOneLineLayout}>
                                    {commInfo?.recruitDeptRecordVO?.fractional}
                                </Form.Item>
                            </Col>
                        </>
                    )}
                    {/* 2021.6.24 理由成为必填项，无需判空 */}
                    <Col span={24}>
                        <Form.Item label="理由" {...oneLineLayout} className={styles.allowWrap}>
                            <div style={{ wordBreak: 'break-all' }}>{commInfo?.recruitDeptRecordVO?.remark}</div>
                        </Form.Item>
                    </Col>
                </>
            );
        }
        return null;
    };

    const hrResult = () => (
        <>
            <Col span={24}>
                <Form.Item colon={false} label={<span style={labelStyle}>人事沟通结果:</span>} {...oneLineLayout}>
                    <span
                        className={cn(styles.hireStatus, {
                            [styles.hireStatusOut]: [
                                COMMUNICATE_RESULT_ENUM.PREOUT,
                                COMMUNICATE_RESULT_ENUM.OUT
                            ].includes(commInfo?.resultType),
                            [styles.hireStatusAgree]: commInfo?.resultType === 1
                        })}
                    >
                        {commInfo?.resultName}
                    </span>
                </Form.Item>
            </Col>
            <Col span={24}>
                <Form.Item label="记录" {...oneLineLayout}>
                    <p style={{ wordBreak: 'break-all' }}>{commInfo?.remark}</p>
                </Form.Item>
            </Col>
        </>
    );

    const viewContent = ( // 如果是二次沟通的记录，人事沟通结果排前面
        <Row>
            {isSecondRecord ? (
                <>
                    {hrResult()}
                    {recruitDeptContent()}
                </>
            ) : (
                <>
                    {recruitDeptContent()}
                    {hrResult()}
                </>
            )}
        </Row>
    );

    // 神奇的设计
    const getCommunicateResultDict = () => {
        if (isSecond) {
            return COMMUNICATE_RESULT.filter(item =>
                [COMMUNICATE_RESULT_ENUM.AGREE, COMMUNICATE_RESULT_ENUM.OUT].includes(item.value)
            );
        }
        return COMMUNICATE_RESULT.filter(item => item.value !== COMMUNICATE_RESULT_ENUM.OUT);
    };

    const communicateResultDict = getCommunicateResultDict();

    // const defaultResultType =
    //     communicateResultDict.find(item => item.value === commInfo?.resultType)?.value ?? COMMUNICATE_RESULT_ENUM.AGREE;

    /**
     * 获取沟通结果初始值
     */
    const getInitResultType = () => {
        if (isSecond) {
            if (commInfo.isSave) {
                return commInfo?.resultType;
            }
            return null;
        }
        return commInfo?.resultType || null;
    };

    const editContent = (
        <Row>
            <Col span={24}>
                <Form.Item label="沟通结果" {...oneLineLayout}>
                    {getFieldDecorator('resultType', {
                        initialValue: getInitResultType(),
                        rules: [
                            {
                                required: true,
                                message: '请选择沟通结果'
                            }
                        ]
                    })(
                        // {getFieldDecorator('resultType', { initialValue: defaultResultType })(
                        <Radio.Group>
                            {communicateResultDict.map(item => (
                                <Radio value={item.value} key={item.value}>
                                    {item.text}
                                </Radio>
                            ))}
                        </Radio.Group>
                    )}
                </Form.Item>
            </Col>
            <Col>
                <Form.Item label="沟通记录" {...oneLineLayout}>
                    {getFieldDecorator('remark', {
                        initialValue: getInintRemark(commInfo, isSecond),
                        rules: [{ max: 500, message: '沟通记录不能超过500个字符' }]
                    })(<TextArea placeholder="请输入沟通记录" style={{ height: 100 }} />)}
                </Form.Item>
            </Col>
        </Row>
    );
    return isEdit ? editContent : viewContent;
}

export default React.memo(Result);
